﻿using Mimas.Core.Current;
using Mimas.Framework.Shared.Data.DataTransferObjects;
using Mimas.Framework.Shared.Data.EnumObjects;
using System;
using System.Linq;

namespace Mimas.Framework.Server.DomainObjects.Mapping
{
    public static class AccessListMapping
    {
        public static AccessList Map(AccessDTO data)
        {
            AccessList newItem = new AccessList();

            newItem.IsAccessable = data.IsAccessable;
            newItem.Id = data.Id;
            newItem.UserTypeId = (int)data.UserType;

            if (data.Id == 0)
            {
                newItem.CreatedBy = UserInfo.CurrentUser.UserId;
                newItem.CreatedOn = DateTime.Now;
            }
            else
            {
                newItem.ModifiedBy = UserInfo.CurrentUser.UserId;
                newItem.ModifiedOn = DateTime.Now;
            }

            return newItem;
        }

        public static AccessDTO Map(AccessList data)
        {
            AccessDTO newItem = new AccessDTO();

            newItem.IsAccessable = data.IsAccessable;
            newItem.Id = data.Id;
            newItem.UserType = (EUserType)data.UserTypeId;

            return newItem;
        }

        public static AccessList[] Map(AccessDTO[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }

        public static AccessDTO[] Map(AccessList[] data)
        {
            if (data == null || !data.Any()) return null;
            return data.Where(x => x != null).Select(x => Map(x)).ToArray();
        }
    }
}
